package min3d.core;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.util.AttributeSet;

import min3d.Shared;
import min3d.interfaces.ISceneController;

public class Min3DView extends GLSurfaceView implements ISceneController {
    public Scene scene;

    protected Handler _initSceneHander;
    protected Handler _updateSceneHander;

    private boolean _renderContinuously;


    final Runnable _initSceneRunnable = new Runnable() {
        public void run() {
            onInitScene();
        }
    };

    final Runnable _updateSceneRunnable = new Runnable() {
        public void run() {
            onUpdateScene();
        }
    };

    private SceneLifecycleListener _sceneLifecycleListener;

    public Min3DView(Context context) {
        this(context, null);
    }

    public Min3DView(Context context, AttributeSet attrs) {
        super(context, attrs);
        _initSceneHander = new Handler();
        _updateSceneHander = new Handler();

        Shared.context(context);
        scene = new Scene(this);
        min3d.core.Renderer r = new min3d.core.Renderer(scene);
        Shared.renderer(r);

        setRenderer(r);

        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    public void setSceneLifecycleListener(SceneLifecycleListener _sceneLifecycleListener) {
        this._sceneLifecycleListener = _sceneLifecycleListener;
    }

    public void onInitScene() {
        if (_sceneLifecycleListener != null) {
            _sceneLifecycleListener.onInitScene();
        }
    }

    public void onUpdateScene() {
        if (_sceneLifecycleListener != null) {
            _sceneLifecycleListener.onUpdateScene();
        }
    }

    public void renderContinuously(boolean $b) {
        _renderContinuously = $b;
        if (_renderContinuously)
            setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

        else
            setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

    @Override
    public void initScene() {

    }

    @Override
    public void updateScene() {

    }

    @Override
    public Handler getInitSceneHandler() {
        return _initSceneHander;
    }

    @Override
    public Runnable getInitSceneRunnable() {
        return _initSceneRunnable;
    }

    @Override
    public Handler getUpdateSceneHandler() {
        return _updateSceneHander;
    }

    @Override
    public Runnable getUpdateSceneRunnable() {
        return _updateSceneRunnable;
    }

    public interface SceneLifecycleListener {
        void onInitScene();
        void onUpdateScene();
    }
}
